const { wordList: WordList } = require('../../../data/wordlewords.js');
//console.log(WordList); 


Page({
  data: {
    targetWord: "", // 目标单词
    attemptsLeft: 6,
    guesses: Array.from({ length: 6 }, () => Array(5).fill({ letter: '', color: '' })),
    currentGuess: [],
    wordLength: 5, // 默认单词长度
    keyboardRow1: [
      { letter: 'Q', color: '' }, { letter: 'W', color: '' }, { letter: 'E', color: '' }, 
      { letter: 'R', color: '' }, { letter: 'T', color: '' }, { letter: 'Y', color: '' },
      { letter: 'U', color: '' }, { letter: 'I', color: '' }, { letter: 'O', color: '' }, 
      { letter: 'P', color: '' }
    ],
    keyboardRow2: [
      { letter: 'A', color: '' }, { letter: 'S', color: '' }, { letter: 'D', color: '' }, 
      { letter: 'F', color: '' }, { letter: 'G', color: '' }, { letter: 'H', color: '' },
      { letter: 'J', color: '' }, { letter: 'K', color: '' }, { letter: 'L', color: '' }
    ],
    keyboardRow3: [
      { letter: 'Z', color: '' }, { letter: 'X', color: '' }, { letter: 'C', color: '' }, 
      { letter: 'V', color: '' }, { letter: 'B', color: '' }, { letter: 'N', color: '' }, 
      { letter: 'M', color: '' }
    ],
    gameStatus: 'playing' // 游戏状态：playing, win, lose
  },

  onLoad() {
    this.setTargetWord(); // 初始化时选择一个单词
  },

  // 随机选择一个指定长度的单词作为目标单词
  setTargetWord() {
    const { wordLength } = this.data;
    const wordsOfLength = WordList.filter(word => word.length === wordLength); // 筛选符合长度的单词
    if (wordsOfLength.length === 0) {
      console.warn(`没有符合长度为 ${wordLength} 的单词`);
      return;
    }
    const randomIndex = Math.floor(Math.random() * wordsOfLength.length);
    const targetWord = wordsOfLength[randomIndex];
    this.setData({ targetWord });
    console.log("目标单词是:", targetWord); // 调试时可以查看目标单词
  },

  // 处理单词长度的选择
  changeWordLength(e) {
    // 获取用户选择的单词长度（假设 e.detail.value 是索引，需要映射到实际长度值）
    const wordLengthOptions = [4, 5, 6, 7]; // 假设提供了4、5、6、7字母的选择
    const wordLength = wordLengthOptions[parseInt(e.detail.value)];
    this.setData({ wordLength }, () => {
      this.setTargetWord(); // 更新目标单词
      this.resetGame(); // 重置游戏
    });
  },

  // 重置游戏
  resetGame() {
    this.setData({
      attemptsLeft: 6,
      guesses: Array.from({ length: 6 }, () => Array(this.data.wordLength).fill({ letter: '', color: '' })),
      currentGuess: [],
      gameStatus: 'playing'
    });
  },

  // 处理按键输入
  handleKeyPress(e) {
    const { key } = e.currentTarget.dataset;
    if (this.data.currentGuess.length < this.data.wordLength && this.data.gameStatus === 'playing') {
      const newGuess = [...this.data.currentGuess, key];
      this.setData({ currentGuess: newGuess });
      this.updateCurrentGuess();
    }
  },

  // 删除最后一个字母
  deleteLetter() {
    if (this.data.currentGuess.length > 0 && this.data.gameStatus === 'playing') {
      const newGuess = this.data.currentGuess.slice(0, -1);
      this.setData({ currentGuess: newGuess });
      this.updateCurrentGuess();
    }
  },

  // 提交当前猜测
  submitGuess() {
    if (this.data.currentGuess.length === this.data.wordLength && this.data.gameStatus === 'playing') {
      const guessWord = this.data.currentGuess.join('').toLowerCase();
      if (guessWord === this.data.targetWord) {
        this.updateGuessColors(this.data.currentGuess, 'green');
        this.setData({ gameStatus: 'win' });
      } else {
        const coloredGuess = this.data.currentGuess.map((letter, i) => {
          if (letter.toLowerCase() === this.data.targetWord[i]) {
            return { letter, color: 'green' };
          } else if (this.data.targetWord.includes(letter.toLowerCase())) {
            return { letter, color: 'yellow' };
          } else {
            return { letter, color: 'gray' };
          }
        });
        const newGuesses = [...this.data.guesses];
        newGuesses[6 - this.data.attemptsLeft] = coloredGuess;

        this.setData({
          guesses: newGuesses,
          attemptsLeft: this.data.attemptsLeft - 1,
          currentGuess: []
        });

        if (this.data.attemptsLeft  === 0) {
          this.setData({ gameStatus: 'lose' });
        }
      }
    }
  },

  updateCurrentGuess() {
    const guessRow = [...this.data.guesses];
    guessRow[6 - this.data.attemptsLeft] = this.data.currentGuess.map(letter => ({ letter, color: '' }));
    this.setData({
      guesses: guessRow
    });
  },

  updateGuessColors(guess, color) {
    const coloredGuess = guess.map(letter => ({ letter, color }));
    const newGuesses = [...this.data.guesses];
    newGuesses[6 - this.data.attemptsLeft] = coloredGuess;

    this.setData({ guesses: newGuesses });
  }
});
